<template>
    <div>
        <a-form :form="form" layout="inline" @submit.prevent="onSubmit" :style="{marginBottom: '20px'}" id="service-menu-search">
            <div class="service-menu-query">
                <a-form-item>
                    <a-input placeholder="菜单名称" v-decorator="['name']"/>
                </a-form-item>
                <a-form-item :style="{flexGrow: 1}">
                    <a-button type="primary"  html-type="submit">
                        筛选
                    </a-button>
                </a-form-item>
                <a-form-item>
                    <router-link to="/servicemenu/create">
                        <a-button type="primary" v-if="menu_role.includes('/servicemenu/create')" icon="plus">添加</a-button>
                    </router-link>
                </a-form-item>
            </div>
        </a-form>
        <a-table :columns="columns" :dataSource="tableData" rowKey="id" :pagination="pagination" bordered>
            <span slot="action" slot-scope="text, record">
                <div class="delete-image">
                <router-link :to="'/servicemenu-update/'+record.id" v-if="menu_role.includes('/servicemenu-update')"><img src="./../../assets/images/view.svg"
                                                                         alt=""></router-link>
                <a href="#" v-if="menu_role.includes('/servicemenu/delete')" @click.stop.prevent="showModal(record)"><img
                        src="./../../assets/images/delete.svg" alt=""></a>
                </div>
            </span>
        </a-table>
        <a-modal
            v-model='visible'
            title='删除菜单'
            @ok = 'handleDelete'
            width="450px"
            :cancelButtonProps="{ props: {type: 'primary'}}"
            centered
            :keyboard = 'false'
            okText = '删除'
            :maskClosable = 'false'
        >
            <div class="confirm-delete-wrapper">
                <p>是否删除此菜单？</p>
            </div>
        </a-modal>
    </div>
</template>

<script>
    export default {
        data() {
            return {
                pagination: {
                    current: 1,
                    total: 0,
                    pageSize: 10,
                    showQuickJumper: true,
                    onChange: (page, size) => {
                        this.handleChange(page, size)
                    },
                },
                tableData: [],
                columns: [
                    {title: 'ID', dataIndex: 'id', align: 'center'},
                    {title: '菜单名称', dataIndex: 'name', align: 'center'},
                    {title: '菜单路径', dataIndex: 'url', align: 'center'},
                    {title: '状态', dataIndex: 'status', align: 'center'},
                    {title: '是否为菜单', dataIndex: 'is_menu', align: 'center'},
                    {title: '操作', scopedSlots: {customRender: 'action'}, align: 'center'},
                ],
                form: this.$form.createForm(this),
                search: {},
                visible: false,
                deleteRow: {}
            }
        },

        mounted() {
            this.getList();
        },
        methods: {
            async getList(param = {}) {
                let req = {page_size: this.pagination.pageSize, page: this.pagination.current};

                let data = await this.http(
                    "get",
                    "servicemenu/index",
                    Object.assign(req, param)
                );
                this.tableData = data.data;
                this.pagination.total = data.total_count;
            },
            showModal(row) {
                this.visible = true;
                this.deleteRow = row;
            },
            onSubmit() {//搜索提交

                this.form.validateFields((err, values) => {
                    if (!err) {
                        this.pagination.current = 1
                        this.search = values
                        this.getList(values)
                    }
                })
            },
            handleChange(page, size) {
                //列表展示条数改变
                this.pagination.current = page
                this.pagination.pageSize = size;
                this.getList(this.search);
            },
            handleDelete() {
                let that = this;
                let row = this.deleteRow;

                that.deleteData(row.id).then(res => {
                    if (res.status == "success") {
                        that.$message.success("删除成功!", 1);
                        that.getList();
                    } else {
                        that.$message.error("删除失败!", 1);
                    }
                    this.visible = false;
                });
            },
            async deleteData(id) {
                return await this.http("delete", "servicemenu/delete", {id: id});
            }
        }
    };
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
    @import "../../assets/scss/mixin";

    #service-menu-search {
        .ant-input {
            width: 250px;
        }
    }

    .confirm-delete-wrapper {
        width: 100%;
        padding: 20px;
        text-align: center;
        p {
            font-size: 16px;
        }
    }

    .service-menu-query {
        @include fj(flex-start);
        .ant-form-item + .ant-form-item{
            margin-left: 10px;
        }
    }
</style>
